<?php

/**
 * TomatoCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@tomatocms.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2009-2010 TIG Corporation (http://www.tig.vn)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @version 	$Id: Item.php 5338 2010-09-07 08:27:25Z huuphuoc $
 * @since		2.0.7
 */
class Menu_Models_Item extends Rabbit_Db_Table_Abstract implements Menu_Models_Interfaces_Item {

    protected $_name = 'menu_item';

    public function convert($entity) {
        return new Menu_Models_Entities_Item($entity);
    }

    public function add($item) {
        try {
            $menuItem = $this->createRow($item->getProperties());
            $menuItem->save();
            if ($menuItem->id) {
                return $menuItem;
            } else {
                return false;
            }
        } catch (Exception $ex) {
            echo $ex->getMessage();
            return false;
        }
    }

    public function update($item){
        $data = $item->getProperties();
        return parent::update($data, 'id = '. $item->id);
    }
    public function delete($menuId) {
        return $this->getApdater()->delete($this->_prefix . 'menu_item', array(
                    'menu_id = ?' => $menuId,
                ));
    }

    public function getTree($menuId) {
        $tree = array();
        $this->getMenuItems(0, $tree, $menuId, 0);
        return new Tomato_Model_RecordSet($tree, $this);
//        $rs = $this->getAdapter()
//                ->select()
//                ->from(array('node' => $this->_prefix . 'menu_item'), array(
//                    'node.item_id', 'node.label', 'node.link', 'node.left_id', 'node.right_id',
//                    'node.parent_id',
//                ))
//                ->from(array('parent' => $this->_prefix . 'menu_item'), array('depth' => '(COUNT(parent.item_id) - 1)'))
//                ->where('node.menu_id = ?', $menuId)
//                ->where('parent.menu_id = ?', $menuId)
//                ->where('node.left_id BETWEEN parent.left_id AND parent.right_id')
//                ->group('node.item_id')
//                ->order('node.left_id')
//                ->query()
//                ->fetchAll();
//        return new Tomato_Model_RecordSet($rs, $this);
    }
    public function getMenuItems($parentId, & $tree, $menuId = null, $depth = null){
        
        $query = $this->getAdapter()
                    ->select()
                    ->from($this->_name)
                    ->where('parent_id = ?', $parentId)
                    ->order('ordering asc');

        if($menuId != null) $query = $query->where('menu_id = ?', $menuId);
        if($depth != null) $query = $query->where('depth = ?', $depth);

        $rs = $query->query()->fetchAll();
        
        if($rs != null && count($rs) > 0){
            if($tree == null) $tree = array();
            foreach($rs as $item){
                $tree[] = $item;
                
                $this->getMenuItems($item->id, $tree);
            }
        }else{
            //var_dump($tree);
            return;
        }
        
    }
    /**
     * Delete menu item by id
     * @param int $id
     * @return bool
     */
    public function deleteById($id){
        return $this->getAdapter()->delete($this->_name, array(
                    'id = ?' => $id,
                ));
    }
    /**
     * Edit menu item
     * @param Menu_Models_Entities_Item $item 
     */
//    public function edit($item) {
//        return $this->update($item->getProperties(), 'id = ' . $item->id);
//    }

}
